package com.kll.carolinehmibiz.presenter

import android.content.Context
import com.google.gson.Gson
import com.kll.carolinehmibiz.base.net.bean.BaseHttpResult
import com.kll.carolinehmibiz.base.net.rx.NetObserver
import com.kll.carolinehmibiz.base.utils.L
import com.kll.carolinehmibiz.model.OperateListener
import com.kll.carolinehmibiz.net.CarolineNetHelper
import com.kll.carolinehmibiz.net.bean.EmptyResponse
import com.kll.carolinehmibiz.net.bean.GetFamilyResponse
import com.kll.carolinehmibiz.net.bean.GetTencentUserIdResponse


/**
 * ====================================================
 *
 * 家人
 *
 * Created by Ziliang Cai on 2021/12/13 21:55
 * <a href="mailto:752777456@qq.com">Contact me</a>
 * ====================================================
 */
class FamilyPresenter : BasePresenter {

    private constructor(context: Context): super(context)

    companion object {

        private var mInstance: FamilyPresenter? = null

        fun getInstance(context: Context): FamilyPresenter {
            if(mInstance == null)
                mInstance = FamilyPresenter(context)
            return mInstance!!
        }
    }

    /**
     * 添加家人
     *
     * @param mobile 手机号
     * @param avatar 头像url
     * @param firstName 姓氏
     * @param lastName 名
     * @param remark 备注
     */
    fun addFamily(mobile: String, avatar: String, firstName: String, lastName: String, remark: String, listener: OperateListener) {

        CarolineNetHelper.getInstance(mContext).addFamily(mobile, avatar, firstName, lastName, remark, object: NetObserver<BaseHttpResult<EmptyResponse?>>() {
            override fun onSuccess(t: BaseHttpResult<EmptyResponse?>) {
                L.d("FamilyPresenter","addFamily>>>onSuccess::${Gson().toJson(t)}")
                listener.onSuccess(t.status,  t.data)
            }

            override fun onFail(code: Int, message: String) {

                L.d("FamilyPresenter","addFamily>>>onFail::$code")

                listener.onFail(code, message)
            }
        })
    }

    /**
     * 编辑家人
     *
     * @param familyId 家人ID
     * @param mobile 手机号
     * @param avatar 头像url
     * @param firstName 姓氏
     * @param lastName 名
     * @param remark 备注
     */
    fun editFamily(familyId: Int,  mobile: String, avatar: String, firstName: String, lastName: String, remark: String, listener: OperateListener){
        CarolineNetHelper.getInstance(mContext).editFamily(familyId, mobile, avatar, firstName, lastName, remark, object: NetObserver<BaseHttpResult<EmptyResponse?>>() {
            override fun onSuccess(t: BaseHttpResult<EmptyResponse?>) {
                L.d("FamilyPresenter","editFamily>>>onSuccess::${Gson().toJson(t)}")
                listener.onSuccess(t.status,  t.data)
            }

            override fun onFail(code: Int, message: String) {
                L.d("FamilyPresenter","editFamily>>>onFail::$code")

                listener.onFail(code, message)
            }

        })
    }

    /**
     * 获取家人列表
     *
     */
    fun getFamily(listener: OperateListener) {

        CarolineNetHelper.getInstance(mContext).getFamily(object: NetObserver<BaseHttpResult<ArrayList<GetFamilyResponse>?>>(){
            override fun onSuccess(t: BaseHttpResult<ArrayList<GetFamilyResponse>?>) {
                L.d("FamilyPresenter","getFamily>>>onSuccess::${Gson().toJson(t)}")
                listener.onSuccess(t.status, t?.data)
            }

            override fun onFail(code: Int, message: String) {
                L.d("FamilyPresenter","getFamily>>>onFail::$code")

                listener.onFail(code, message)
            }

        })
    }

    /**
     * 删除家人
     * @param familyId 家人ID
     */
    fun delFamily(familyId: Int, listener: OperateListener) {
        CarolineNetHelper.getInstance(mContext).delFamily(familyId, object: NetObserver<BaseHttpResult<String?>>() {
            override fun onSuccess(t: BaseHttpResult<String?>) {
                L.d("FamilyPresenter","delFamily>>>onSuccess::${Gson().toJson(t)}")
                listener.onSuccess(t.status, t?.data)
            }

            override fun onFail(code: Int, message: String) {
                L.d("FamilyPresenter","delFamily>>>onFail::$code")

                listener.onFail(code, message)
            }

        })
    }

    fun getTencentId(tencentUserId: String, listener: OperateListener) {
        CarolineNetHelper.getInstance(mContext).getTencentId(tencentUserId, object : NetObserver<BaseHttpResult<GetTencentUserIdResponse>>() {
            override fun onSuccess(t: BaseHttpResult<GetTencentUserIdResponse>) {
                listener.onSuccess(t.status, t.data)
            }

            override fun onFail(code: Int, message: String) {
                listener.onFail(code, message)
            }

        })
    }


}